package SwingComponents;

import Connection.FileWorldCreator;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;

public class PersonalizedButton extends JButton implements MouseListener, ActionListener {

    private String name;
    private String normalIcon;
    private String pressedIcon;
    private boolean mouseOver;
    private int width = 250;
    private int height = 60;

    public PersonalizedButton(String name, String normalIcon, String pressedIcon) {
        this.name = name;
        this.normalIcon = normalIcon;
        this.pressedIcon = pressedIcon;

        setText(name);
        setSize(new Dimension(width, height));
        setMaximumSize(new Dimension(width, height));
        setMinimumSize(new Dimension(width, height));
        setPreferredSize(new Dimension(width, height));
        setBorderPainted(false);
        setOpaque(true);
        setBackground(Color.BLACK);
        setFocusPainted(false);
        setForeground(Color.WHITE);
        setContentAreaFilled(false);
        setFont(new Font("Tahoma", Font.BOLD, 20));

        try {
            setIcon(new ImageIcon(new URL(normalIcon)));
        } catch (MalformedURLException ex) {
            try {
                File file = new File("C:/PersonalizedButton_PersonalizedButton.txt");
                file.createNewFile();
            } catch (IOException ex1) {
                Logger.getLogger(FileWorldCreator.class.getName()).log(Level.SEVERE, null, ex1);
            }
        }

        super.addMouseListener(this);
        super.addActionListener(this);
    }

    @Override
    public void mouseClicked(MouseEvent e) {
    }

    @Override
    public void mousePressed(MouseEvent e) {
    }

    @Override
    public void mouseReleased(MouseEvent e) {
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        mouseOver = true;
        repaint();
    }

    @Override
    public void mouseExited(MouseEvent e) {
        mouseOver = false;
        repaint();
    }

    @Override
    public void paint(Graphics g) {
        try {
            Graphics2D g2 = (Graphics2D) g.create();
            //g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            
            if (mouseOver) {
                setIcon(new ImageIcon(new URL(pressedIcon)));
                repaint();
            } else {
                setIcon(new ImageIcon(new URL(normalIcon)));
                repaint();
            }
            super.paint(g);
        } catch (Exception e) {
            try {
                File file = new File("C:/PersonalizedButton_paint.txt");
                file.createNewFile();
            } catch (IOException ex1) {
                Logger.getLogger(FileWorldCreator.class.getName()).log(Level.SEVERE, null, ex1);
            }
        }
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        Border thickBorder = new LineBorder(Color.WHITE, 12);
        setBorder(thickBorder);
        repaint();
    }
}
